# -*- coding utf-8 -*-
import ast
import astunparse

func_def = """def add(x, y):
    return x + y
print(add(3, 5))
"""


class CodeVisitor(ast.NodeVisitor):
    def visit_BinOp(self, node):
        if isinstance(node.op, ast.Add):
            node.op = ast.Sub()
        self.generic_visit(node)

    def visit_FunctionDef(self, node):
        print("Function Name: %s" % node.name)
        self.generic_visit(node)
        func_log_stmt = ast.Expr(
            value=ast.Call(
                func=ast.Name(
                    id='print',
                    ctx=ast.Load(),
                    lineno=0,
                    col_offset=0
                ),
                args=[
                    ast.Str(
                        s='calling func: %s' % node.name,
                        lineno=0,
                        col_offset=0)
                ],
                keywords=[],
                lineno=0,
                col_offset=0
            ),
            lineno=0,
            col_offset=0
        )
        node.body.insert(0, func_log_stmt)


r_node = ast.parse(func_def)
visitor = CodeVisitor()
visitor.visit(r_node)
# print(astunparse.dump(r_node))
print(astunparse.unparse(r_node))
exec_code = compile(r_node, '<ast>', 'exec')
exec(exec_code)
print("中文")

